package com.nanyang.tms.manager.waybill.shipment.handler;

import java.util.Collections;
import java.util.List;

import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.nanyang.common.core.dict.CommonEnum;
import com.nanyang.common.core.flow.context.TransHandlerContext;
import com.nanyang.common.core.flow.handler.AbstractTransHandler;
import com.nanyang.common.tool.utils.convert.BeanUtils;
import com.nanyang.tms.common.exception.TmsManagerException;
import com.nanyang.tms.common.util.TmsExceptionUtil;
import com.nanyang.tms.dao.waybill.WaybillDao;
import com.nanyang.tms.domain.cost.context.CalculateCostFreightContext;
import com.nanyang.tms.domain.waybill.WaybillDetailVO;
import com.nanyang.tms.domain.waybill.WaybillDetailVolumeVO;
import com.nanyang.tms.domain.waybill.shipment.context.CalculateSupplierWeightContext;
import com.nanyang.tms.domain.waybill.shipment.context.CalculateSupplierWeightVolumeContext;
import com.nanyang.tms.domain.waybill.shipment.context.CheckSupplierNoContext;

import cn.hutool.core.collection.CollUtil;
@Component
public class CheckSupplierNoHandler extends AbstractTransHandler{
    @Autowired
    private WaybillDao waybillDao;
	@Override
	public boolean isAsync() {
		return false;
	}

	@Override
	public boolean handle(TransHandlerContext context) {
		CheckSupplierNoContext checkSupplierNoContext=(CheckSupplierNoContext) context.getInnerContext(CheckSupplierNoContext.class);
		try {
			WaybillDetailVO waybillDetailVO = waybillDao.getWaybillDetailBySupplierNo(checkSupplierNoContext.getSupplierNo());
			if(ObjectUtils.isEmpty(waybillDetailVO)){
				throw new TmsManagerException(CommonEnum.NO_CONTENT.getCode(), CommonEnum.NO_CONTENT.getMessage());
			}
			List<WaybillDetailVolumeVO> waybillDetailVolumes = waybillDao.getWaybillDetailVolumeBySupplierNo(checkSupplierNoContext.getSupplierNo());
			if(CollUtil.isEmpty(waybillDetailVolumes)){
				throw new TmsManagerException(CommonEnum.NO_CONTENT.getCode(), CommonEnum.NO_CONTENT.getMessage());
			}
			
			CalculateSupplierWeightContext  calculateSupplierWeightContext=	(CalculateSupplierWeightContext) context.getInnerContext(CalculateSupplierWeightContext.class);
			BeanUtils.copyProperties(waybillDetailVO, calculateSupplierWeightContext);
			BeanUtils.copyListProperties(waybillDetailVolumes, calculateSupplierWeightContext.getCalculateSupplierWeightVolumeContext(), CalculateSupplierWeightVolumeContext.class);
		} catch (Exception e) {
			throw TmsExceptionUtil.wrapException(e);
		}
		return true;
	}

}
